package cn.mst.vrs.controller.admin.venue;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.mst.vrs.controller.admin.venue.vo.VenuePageReqVO;
import cn.mst.vrs.controller.admin.venue.vo.VenueRespVO;
import cn.mst.vrs.controller.admin.venue.vo.VenueSaveReqVO;
import cn.mst.vrs.dal.dataobject.venue.VenueDO;
import cn.mst.vrs.service.venue.VenueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 体育场馆")
@RestController
@RequestMapping("/vrs/venue")
@Validated
public class VenueController {

    @Resource
    private VenueService venueService;

    @PostMapping("/create")
    @Operation(summary = "创建体育场馆")
    @PreAuthorize("@ss.hasPermission('vrs:venue:create')")
    public CommonResult<Long> createVenue(@Valid @RequestBody VenueSaveReqVO createReqVO) {
        return success(venueService.createVenue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新体育场馆")
    @PreAuthorize("@ss.hasPermission('vrs:venue:update')")
    public CommonResult<Boolean> updateVenue(@Valid @RequestBody VenueSaveReqVO updateReqVO) {
        venueService.updateVenue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除体育场馆")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vrs:venue:delete')")
    public CommonResult<Boolean> deleteVenue(@RequestParam("id") Long id) {
        venueService.deleteVenue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得体育场馆")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vrs:venue:query')")
    public CommonResult<VenueRespVO> getVenue(@RequestParam("id") Long id) {
        VenueDO venue = venueService.getVenue(id);
        return success(BeanUtils.toBean(venue, VenueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得体育场馆分页")
    @PreAuthorize("@ss.hasPermission('vrs:venue:query')")
    public CommonResult<PageResult<VenueRespVO>> getVenuePage(@Valid VenuePageReqVO pageReqVO) {
        PageResult<VenueDO> pageResult = venueService.getVenuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VenueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出体育场馆 Excel")
    @PreAuthorize("@ss.hasPermission('vrs:venue:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVenueExcel(@Valid VenuePageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VenueDO> list = venueService.getVenuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "体育场馆.xls", "数据", VenueRespVO.class,
                BeanUtils.toBean(list, VenueRespVO.class));
    }

}